package cn.zhuama.basic.service.impl;

import cn.zhuama.basic.mapper.BaseMapper;
import cn.zhuama.basic.query.BaseQuery;
import cn.zhuama.basic.service.IBaseService;
import cn.zhuama.basic.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> loadAll() {
        return baseMapper.selectAll();
    }

    @Override
    public T londOne(Long id) {
        return baseMapper.selectOne(id);
    }

    @Override
    @Transactional
    public void editById(T t) {
        baseMapper.updateById(t);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.insert(t);
    }

    @Override
    public void removeById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        Long totalCount = baseMapper.selectTotalCount(baseQuery);//高级查询计算总条数
        if(totalCount<=0){
            return new PageList<T>();  //当我们查询总条数为0，返回空的PageList ,并且没有必要执行下面的page查询
        }
        List<T> page = baseMapper.page(baseQuery);
        PageList<T> pageList = new PageList<>(totalCount, page);
        return pageList;
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {

        baseMapper.patchDel(ids);
    }

}
